package com.fzy.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 日志拦截器 拦截Controller方法 打印日志
 *
 * @author 冯赵杨
 * @date 2017年11月3日
 */
@Aspect
@Component
public class RequestLogAspect {

    private static final Logger log = LoggerFactory.getLogger(RequestLogAspect.class);

    /**
     * defined aop pointcut
     *
     * @param
     * @return
     * @author 冯赵杨
     * @create 2018/8/26
     */
    @Pointcut("execution(* com.fzy.*.*(..))")
    public void controllerLog() {
    }

    /**
     * log all of controller
     *
     * @param joinPoint
     */
    @Before("controllerLog()")
    public void before(JoinPoint joinPoint) {
        String targetName = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();

        log.info("url={}.{},params= {} ", targetName, methodName, arguments);
    }

    @AfterReturning(pointcut = "controllerLog()", returning = "retVal")
    public void after(JoinPoint joinPoint, Object retVal) {
        //log.info("response={}", retVal);
    }
}
